/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.dao;

import java.awt.geom.Point2D;

import org.jdom.Element;

import who.tmap.dao.TMapDAOException;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;
import who.tmap.toolkit.shape.annotation.TextShape;

/**
 * @author stl
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TextShapeDAO extends BaseMapShapeDAO {

	public MapShape load( Element elt ) throws TMapDAOException {
	    TextShape textShape =null;
	    
        try {
            textShape = (TextShape)msf.getGraphic( MapShapes.TEXT );
            
            Element colorElt =elt.getChild( "Color" );
            textShape.setColor( getColorFromHexa( colorElt.getAttribute( "rgb" ).getValue() ) );
            
            Element ptElt =elt.getChild( "Point" );
            Point2D pt =new Point2D.Double( 
                    			ptElt.getAttribute( "x" ).getDoubleValue(),
                    			-ptElt.getAttribute( "y" ).getDoubleValue() );
            textShape.setPt( pt );
            
            textShape.setText( elt.getChildText( "Text" ) );
            
            Element fontElt =elt.getChild( "Font" );
            textShape.setFontName( fontElt.getAttribute( "name" ).getValue() );
            textShape.setFontSize( fontElt.getAttribute( "size" ).getIntValue() );
            textShape.setFontStyle( fontElt.getAttribute( "style" ).getIntValue() );
        } catch (Exception e) {
            throw new TMapDAOException( e );
        }
        return textShape;
	}
	
	public Element save( MapShape mapShape ) throws TMapDAOException {
	    Element shapeElt =new Element( MapShapes.names[ MapShapes.TEXT ] );
	    
        TextShape textShape =(TextShape)mapShape;
        
        Element ptElt =new Element( "Point" );
        shapeElt.addContent( ptElt );

        ptElt.setAttribute( "x", Double.toString( textShape.getPt().getX() ) );
        ptElt.setAttribute( "y", Double.toString( -textShape.getPt().getY() ) );
        
        Element colorElt =new Element( "Color" );
        shapeElt.addContent( colorElt );
        colorElt.setAttribute( "rgb", Integer.toHexString( textShape.getColor().getRGB() ) );
        
        Element textElt =new Element( "Text" );
        shapeElt.addContent( textElt );
        textElt.setText( textShape.getText() );
        
        Element fontElt =new Element( "Font" );
        shapeElt.addContent( fontElt );
        fontElt.setAttribute( "name", textShape.getFontName() );
        fontElt.setAttribute( "size", String.valueOf( textShape.getFontSize() ) );
        fontElt.setAttribute( "style", String.valueOf( textShape.getFontStyle() ) );

        return shapeElt;
	}
    
}
